# encoding:utf-8
from __future__ import division
import time
import datetime
import requests
from constant import constant_define
import re
from dao.MySqlDAL import MySqlDAL
import utils.CookieUtils as cu
'''

雪球
http://xueqiu.com/v4/stock/quote.json?code=SH601988,SH600000

{"SH601988":{"symbol":"SH601988","exchange":"SH","code":"601988","name":"中国银行",
"current":"4.02","percentage":"-2.19","change":"-0.09","open":"4.11","high":"4.11","low":"3.95",
"close":"4.02","last_close":"4.11","high52week":"5.96","low52week":"3.07","volume":"4.32924365E8",
"volumeAverage":"300972141","marketCapital":"1.18343892078882E12","eps":"0.43","pe_ttm":"6.9611",
"pe_lyr":"6.978","beta":"0.0","totalShares":"294387791241","time":"Tue Dec 01 14:59:58 +0800 2015",
"afterHours":"0.0","afterHoursPct":"0.0","afterHoursChg":"0.0",
"afterHoursTime":"Wed Oct 19 21:59:01 -0400 2011","updateAt":"1448884807267",
"dividend":"0.19","yield":"4.73","turnover_rate":"0.21","instOwn":"0.0","rise_stop":"4.52",
"fall_stop":"3.7","currency_unit":"CNY","amount":"1.740692989E9","net_assets":"3.9374",
"hasexist":"","has_warrant":"0","type":"11","flag":"1","rest_day":"","amplitude":"3.89",
"lot_size":"100","tick_size":"0.01","kzz_stock_symbol":"03988","kzz_stock_name":"",
"kzz_stock_current":"0.0","kzz_convert_price":"0.0","kzz_covert_value":"0.0","kzz_cpr":"0.0",
"kzz_putback_price":"0.0","kzz_convert_time":"","kzz_redempt_price":"0.0",
"kzz_straight_price":"0.0","kzz_stock_percent":"","pb":"1.02","benefit_before_tax":"0.0",
"benefit_after_tax":"0.0","convert_bond_ratio":"","totalissuescale":"","outstandingamt":"",
"maturitydate":"","remain_year":"","convertrate":"","interestrtmemo":"",
"release_date":"","circulation":"0.0","par_value":"0.0","due_time":"0.0",
"value_date":"","due_date":"","publisher":"","redeem_type":"T","issue_type":"1",
"bond_type":"","warrant":"","sale_rrg":"","rate":"","after_hour_vol":"0",
"float_shares":"210765512704","float_market_capital":"8.4727736107008E11",
"disnext_pay_date":"","convert_rate":"","psr":"2.5354"}}
'''


class StockPriceXueQiuCrawler(object):
    __target = ""
    __page_size = 50
    __sqlUtil = MySqlDAL()
    __tablename = 'tb_stock_price'

    __headers = {
            'Accept':'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding':'gzip, deflate, sdch',
            'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
            'cache-control':'no-cache',
            'Connection':'keep-alive',
            'User-Agent': constant_define.user_agent,
            'Host':'xueqiu.com',
            'X-Requested-With':'XMLHttpRequest',
            'Cookie': cu.init_cookie()
    }


    # 设置目标网址
    def set_target(self, target):
        self.__target = target


    # 将股票代码分组，每组最多600个 [[],[],[]...]
    def get_stock_list(self):
        query_sql = r'SELECT stock_id FROM tb_stock_info'
        res_dict = self.__sqlUtil.get_dimensions_rows(query_sql)
        params = list()
        slen = len(res_dict)
        count = slen//self.__page_size
        for i in xrange(0,count):
            param = res_dict[self.__page_size*i:self.__page_size*(i+1)]
            params.append(param)

        if slen%self.__page_size > 0 :
            param = res_dict[self.__page_size*count:]
            params.append(param)

        return params


    # 处理请求返回json
    def get_request(self,req_url):
        req_json = requests.get(req_url,headers=self.__headers).json()
        res_json = dict()
        for key,value in req_json.iteritems():
            tmp = dict()
            tmp['stock_id'] = key
            tmp['price'] = float(value['current'])
            tmp['percent'] = (float(value['percentage']))/100
            tmp['open'] = float(value['open'])
            tmp['updown'] = float(value['change'])
            tmp['yestclose'] = float(value['last_close'])
            tmp['volume'] = float(value['volume'])
            tmp['turnover'] = float(value['amount'])
            if float(value['float_shares']) < 0.01:
                tmp['turnover_rate'] = 0
            else:
                tmp['turnover_rate'] = tmp['turnover']/float(value['float_shares'])
            tmp['float_shares'] = float(value['float_shares'])
            if str(value['pe_lyr']) == '':
                tmp['pe_lyr'] = 0
            else:
                tmp['pe_lyr'] = float(value['pe_lyr'])
            if str(value['pe_ttm']) == '':
                tmp['pe_ttm'] = 0
            else:
                tmp['pe_ttm'] = float(value['pe_ttm'])
            if str(value['pb']) == '':
                tmp['pb'] = 0
            else:
                tmp['pb'] = float(value['pb'])
            if str(value['psr']) == '':
                tmp['mr'] = 0
            else:
                tmp['mr'] = float(value['psr'])

            res_json[key] = tmp

        return res_json


    # 获取并设置网页内容
    def get_stock_info_json(self):
        stock_group = self.get_stock_list()
        stock_json = dict()
        for item in stock_group:
            param = ''
            for row in item:
                param += str(row['stock_id'])+','
            param = param[:len(param)-1]
            stock_json.update(self.get_request(self.__target+param))
            # 防止频繁访问网站
        return stock_json

    # 获取股票流通市值
    def update_market_value(self):
        res_list = self.get_stock_info_json()

        data_keys = ['market_value','update_time']
        filter_collection_key = ['stock_id']
        data_source = []
        for key,value in res_list.iteritems():
            tmp = list()
            tmp.append(value['float_shares'])
            tmp.append(time.time())
            tmp.append(key)
            data_source.append(tmp)

        return self.__sqlUtil.update_many_batch(data_keys,data_source,'tb_stock_info',filter_collection_key,80)


    # 写db
    def writeToDB(self):
        data_keys = ['stock_id','price','percent','open','updown','yestclose','volume','turnover','turnover_rate','pe_lyr','pe_ttm','pb','mr','date','update_time']
        data_source = []
        for key,value in self.get_stock_info_json().iteritems():
            tmp = list()
            tmp.append(key)
            tmp.append(value['price'])
            tmp.append(value['percent'])
            tmp.append(value['open'])
            tmp.append(value['updown'])
            tmp.append(value['yestclose'])
            tmp.append(value['volume'])
            tmp.append(value['turnover'])
            tmp.append(value['turnover_rate'])
            tmp.append(value['pe_lyr'])
            tmp.append(value['pe_ttm'])
            tmp.append(value['pb'])
            tmp.append(value['mr'])
            tmp.append(datetime.date.today())
            tmp.append(time.time())
            data_source.append(tmp)

        return self.__sqlUtil.insert_many_batch(data_keys,data_source,self.__tablename,50)



    # 更新 db
    def updateDB(self):
        data_keys = ['price','percent','open','updown','yestclose','volume','turnover','turnover_rate','update_time']
        data_source = []
        filter_collection_key = ['stock_id','date']
        for key,value in self.get_stock_info_json().iteritems():
            tmp = list()
            tmp.append(value['price'])
            tmp.append(value['percent'])
            tmp.append(value['open'])
            tmp.append(value['updown'])
            tmp.append(value['yestclose'])
            tmp.append(value['volume'])
            tmp.append(value['turnover'])
            tmp.append(value['turnover_rate'])
            tmp.append(value['pe_lyr'])
            tmp.append(value['pe_ttm'])
            tmp.append(value['pb'])
            tmp.append(value['mr'])
            tmp.append(time.time())

            tmp.append(key)
            tmp.append(datetime.date.today())
            data_source.append(tmp)

        return self.__sqlUtil.update_many_batch(data_keys,data_source,self.__tablename,filter_collection_key,50)



# a = StockPriceXueQiuCrawler();
# a.set_target("http://xueqiu.com/v4/stock/quote.json?code=")
# count = a.get_stock_info_json()